package chapter_12_flyweight.classic;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by CharloYao on 2017/10/13.
 * 享元工厂
 */
public class FlyweightFactory {
    //定义一个池容器
    private static Map<String, Flyweight> map = new HashMap<>();

    //享元工厂
    public static Flyweight getFlyweight(String extrinsic) {
        //需要返回的对象
        Flyweight flyweight = null;
        //池中有该对象
        if (map.containsKey(extrinsic)) {
            flyweight = map.get(extrinsic);
        } else {
            //池中没有该对象，创建新的对象
            flyweight = new ConcreteFlyweight1(extrinsic);
            //放入池中
            map.put(extrinsic, flyweight);
        }
        return flyweight;
    }
}
